package cn.com.self.apis.portal.service.impl;

import cn.com.self.apis.portal.entity.DatatypeEntity;
import cn.com.self.apis.portal.mapper.DatatypeGroupMapper;
import cn.com.self.apis.portal.service.intf.IDatatypeService;
import cn.com.self.apis.portal.util.RedisCodeUtil;
import cn.com.self.apis.portal.util.RedisUtils;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 数据类型 服务实现类
 */
@Service
public class DatatypeServiceImpl extends BaseServiceImpl<DatatypeEntity, String> implements IDatatypeService {
    @Autowired
    private DatatypeGroupMapper datatypeGroupMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<DatatypeEntity> zdList(String code) {
        return datatypeGroupMapper.zdList(code);
    }

    @Override
    public List<DatatypeEntity> getListBygroupIdOrgrouptype(Long groupId, String grouptype) {
        return datatypeGroupMapper.getListBygroupIdOrgrouptype(groupId,grouptype);
    }

    /**
     * 更新redis字典缓存
     * @param d
     * @param nodelete
     */
    @Override
    public void updateRedisDatatype(DatatypeEntity d, boolean nodelete) {
        try {
            String grouptype=datatypeGroupMapper.selectById(d.getGroupId()).getGrouptype();
            List<DatatypeEntity> list=datatypeGroupMapper.getListBygroupIdOrgrouptype(null,grouptype);
            if(nodelete){
                redisUtils.lSet(RedisCodeUtil.list_datatype_+grouptype,list);
                redisUtils.set(grouptype+d.getCode(),d);
                redisUtils.set(RedisCodeUtil.datatype_name+grouptype+d.getCode(),d.getName());
            }else {
                redisUtils.del(grouptype+d.getCode());
                redisUtils.del(RedisCodeUtil.datatype_name+grouptype+d.getCode());
                redisUtils.lSet(RedisCodeUtil.list_datatype_+grouptype,list);
            }
        }catch (Exception e){
            System.out.println(e);
        }

    }


}
